package com.primaryedu.ui.app.leave;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.primaryedu.R;
import com.primaryedu.data.Contants;
import com.primaryedu.photo.PhotoPreviewIntent;
import com.primaryedu.ui.BaseActivity;
import com.primaryedu.widget.MyGridView;

import java.util.ArrayList;

/**
 * Created by Thomas on 2018/1/29.
 */

public class LeaveDetailsActivtiy extends BaseActivity implements View.OnClickListener {

    private TextView tv_leavetype_approve;
    private TextView tv_leavereanson;
    private RadioGroup radioGroup;
    private int approveId;
    private RadioButton rb_disagree_details;
    private RadioButton rb_agree_details;
    private EditText edit_approveopinion;
    private TextView tv_endtime_deatils;
    private TextView tv_starttime;
    private ImageView iv_pic1;
    private ImageView iv_pic2;
    private ImageView iv_pic3;
    private GridLeaveDeatailsAdapter gridAdapter;
    private ArrayList<String> imagePaths = new ArrayList<>();
    private MyGridView gridView;
    private static final int REQUEST_PREVIEW_CODE = 20;
    private EditText edit_approveopinion_details;
    private TextView tv_auditorName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_leave_details);

        init();
    }

    private void init() {
        Intent intent = getIntent();
        //处理状态
        int leaveStaus = intent.getIntExtra("staus", 0);
        Log.i("yy", "init: leaveStaus=====" + leaveStaus);
        //开始时间
        String leaveStarttime = intent.getStringExtra("starttime");
        //结束时间
        String leaveEndtime = intent.getStringExtra("endtime");
        String leavetype = intent.getStringExtra("type");
        String leavereason = intent.getStringExtra("reason");
        Log.i("yy", "init: leavereason=====" + leavereason);
        //请假图片 可无
        String pic1 = intent.getStringExtra("pic1");
        String pic2 = intent.getStringExtra("pic2");
        String pic3 = intent.getStringExtra("pic3");
        Log.i("yy", "init: pic1=====" + pic1.toString());
        Log.i("yy", "init: pic2=====" + pic2);
        Log.i("yy", "init: pic3=====" + pic3);
        //审批人auditorname
        String auditorname = intent.getStringExtra("auditorname");
        approveId = intent.getIntExtra("id", 0);
        ((TextView) findViewById(R.id.title)).setText("请假审批");
        findViewById(R.id.back).setOnClickListener(this);

        //审批意见
        String leaveremark = intent.getStringExtra("remark");
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup_details);
        disableRadioGroup(radioGroup);
        rb_agree_details = (RadioButton) findViewById(R.id.rb_agree_details);
        rb_disagree_details = (RadioButton) findViewById(R.id.rb_disagree_details);
        edit_approveopinion_details = (EditText) findViewById(R.id.edit_approveopinion_details);
        edit_approveopinion_details.setText(leaveremark);
        ImageView image_leave_details = (ImageView) findViewById(R.id.image_leave_details);
        if (leaveStaus == 1) {
            image_leave_details.setBackgroundResource(R.drawable.image_agree);
            rb_agree_details.setChecked(true);
        } else if (leaveStaus == 0) {
            image_leave_details.setBackgroundResource(R.drawable.leave_no_deal);
        } else if (leaveStaus == 2) {
            image_leave_details.setBackgroundResource(R.drawable.image_disagree);
            rb_disagree_details.setChecked(true);
        }
        //请假类型
        tv_leavetype_approve = (TextView) findViewById(R.id.tv_leavetype_details);
        tv_leavetype_approve.setText(leavetype);
        //开始时间
        tv_starttime = (TextView) findViewById(R.id.tv_starttime_deatils);
        tv_starttime.setText(leaveStarttime);
        //结束时间
        tv_endtime_deatils = (TextView) findViewById(R.id.tv_endtime_deatils);
        tv_endtime_deatils.setText(leaveEndtime);

        //请假原因
        tv_leavereanson = (TextView) findViewById(R.id.tv_leavereanson_details);
        tv_leavereanson.setText(leavereason);
        //请假图片
        if (!pic1.equals("")) {
            imagePaths.add(Contants.course_api + pic1);
        }
        if (!pic2.equals("")) {
            imagePaths.add(Contants.course_api + pic2);
        }
        if (!pic3.equals("")) {
            imagePaths.add(Contants.course_api + pic3);
        }
        gridView = (MyGridView) this.findViewById(R.id.gridView_leavedetails);
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi;
        cols = cols < 3 ? 3 : cols;
        gridView.setNumColumns(cols);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String imgs = (String) parent.getItemAtPosition(position);
//					Toast.makeText(DynamicPublishActivity.this, "1" + position, Toast.LENGTH_SHORT).show();
                PhotoPreviewIntent intent = new PhotoPreviewIntent(LeaveDetailsActivtiy.this);
                intent.setCurrentItem(position);
                //如果不加这行会导致选完图片以后在主界面点击图片预览会多出一张没有default_error 的图片
//                    imagePaths.remove(imagePaths.size()-1);
                intent.setPhotoPaths(imagePaths);
                startActivityForResult(intent, REQUEST_PREVIEW_CODE);
            }
        });

        gridAdapter = new GridLeaveDeatailsAdapter(imagePaths);
        gridView.setAdapter(gridAdapter);

        //审批人
        tv_auditorName = (TextView) findViewById(R.id.tv_auditorName);
        tv_auditorName.setText(auditorname);
        //审批意见
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup_details);
        disableRadioGroup(radioGroup);
        rb_agree_details = (RadioButton) findViewById(R.id.rb_agree_details);
        rb_disagree_details = (RadioButton) findViewById(R.id.rb_disagree_details);
//        rb_agree_details.setChecked(false);
//        rb_disagree_details.setChecked(false);

        //意见内容
        edit_approveopinion = (EditText) findViewById(R.id.edit_approveopinion_details);

    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_leave_details;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    public void disableRadioGroup(RadioGroup testRadioGroup) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(false);
        }
    }

    public void enableRadioGroup(RadioGroup testRadioGroup) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    public class GridLeaveDeatailsAdapter extends BaseAdapter {

        private ArrayList<String> listUrls;
        private LayoutInflater inflater;

        public GridLeaveDeatailsAdapter(ArrayList<String> listUrls) {
            this.listUrls = listUrls;
            if (listUrls.size() == 7) {
                listUrls.remove(listUrls.size() - 1);
            }
            inflater = LayoutInflater.from(LeaveDetailsActivtiy.this);
        }

        public int getCount() {
            return listUrls.size();
        }

        @Override
        public String getItem(int position) {
            return listUrls.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.include_item_published_grida, parent, false);
                holder.image = (ImageView) convertView.findViewById(R.id.item_grida_image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final String path = listUrls.get(position);

            Glide.with(LeaveDetailsActivtiy.this)
//						.load(Contants.course_api + path)
                    .load(path)
                    .placeholder(R.drawable.default_error)
                    .error(R.drawable.default_error)
                    .centerCrop()
                    .crossFade()
                    .into(holder.image);
            return convertView;
        }

        class ViewHolder {
            ImageView image;
        }

    }
}
